/**
 * 游戏倒计时
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-24 11:07
 * 
 */
package com.addcn.texas.view.components
{
	import flash.display.DisplayObject;	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;	
	import com.addcn.common.utils.AppLibary;
	
	
	public class  GameClock extends Sprite
	{
		private static var _instance : GameClock;
		public static const TIME_EVENT : String = "timeEvent";
		private var _class : Class;
		private var _clock : MovieClip;
		private var _timer : Timer;
		
		public function GameClock() : void
		{
			if (_instance != null) {
				throw("[GameClock] 为单例类");		
				return;
			}			
		}
		
		public static function getInstance() : GameClock 
		{
			if (GameClock._instance == null) {
				GameClock._instance = new GameClock;				
			}
			return GameClock._instance;
		}
		
		public function initialize( ) : void
		{				
			this._class = AppLibary.getInstance().getClass("TimeBar");	
			this._timer = new Timer(1000, 21);
			this._timer.addEventListener(TimerEvent.TIMER, _timerHandler);						
			if(!this.getChildByName("clock")){
				this._clock = new _class;			
				this._clock.x = 0;
				this._clock.y = 0;	
				_clock.name = "clock";
				this.addChild(this._clock);			
			}
		}
		
		public function timeStart() : void
		{
			this._timer.reset();
			this._clock.gotoAndStop(1);
			this._timer.start();
		}
		
		public function timeStop() : void
		{
			this._timer.stop();
			this._timer.reset();
			this._timer.removeEventListener(TimerEvent.TIMER, _timerHandler);						
		}
		

		public function show(r_x : int, r_y : int) : void
		{
			this.x = r_x;
			this.y = r_y;
		
		}
		
		private function _timerHandler(evt : TimerEvent) : void
		{						
			if( this._timer.currentCount <=20){
				this._clock.gotoAndStop(this._timer.currentCount);
			}
			if (this._timer.currentCount == 21) {
				this._timer.stop();
				this._timer.reset();	
				//this.parent.removeChild(this);
				var _event : Event = new Event(GameClock.TIME_EVENT);
				dispatchEvent(_event);
			}			
			
		}
		
		public function destroy() : void
		{
			this._timer.removeEventListener(TimerEvent.TIMER, _timerHandler);			
			this._timer.stop();			
			this._timer = null;
		}
	}
	
}
